package com.oa.lms.apps.business.absence.impl;

import java.util.Date;
import java.util.List;

import com.oa.lms.apps.business.absence.IAbsenceCategoryService;
import com.oa.lms.apps.business.absence.IAbsenceOrderService;
import com.oa.lms.apps.business.absence.IProcessesService;
import com.oa.lms.apps.business.absence.IReplyOrderService;
import com.oa.lms.apps.business.notice.IMessageInfoService;
import com.oa.lms.apps.business.user.IUsersService;
import com.oa.lms.apps.constants.IDEDStatus;
import com.oa.lms.apps.constants.IOrderStatus;
import com.oa.lms.apps.dao.absence.IAbsenceOrderDao;
import com.oa.lms.apps.entity.absence.AbsenceOrderValue;

public class AbsenceOrderServiceImpl implements IAbsenceOrderService {
	
	private IAbsenceOrderDao absenceOrderDao;
	
	private AbsenceOrderValue absenceOrderValue;
	
	private IAbsenceCategoryService absenceCategoryService;
	
	private IMessageInfoService messageInfoService;
	
	private IProcessesService prcoessService;
	
	private IReplyOrderService replyOrderService;
	
	private IUsersService usersService;

	@Override
	public Integer add(String title, String reason, Date startTime,
			Date endTime, Integer totalHours,
			Integer categoryId, Integer userId) {
		absenceOrderValue.setTitle(title);
		absenceOrderValue.setReason(reason);
		absenceOrderValue.setStartTime(startTime);
		absenceOrderValue.setEndTime(endTime);
		absenceOrderValue.setTotalHours(totalHours);
		absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		absenceOrderValue.setStatus(IOrderStatus.PREPARE);
		absenceOrderValue.setCreateTime(new Date());
		Integer id = absenceOrderDao.add(absenceOrderValue);
		if(id != null){
			messageInfoService.add("请假单提交成功", "["+title+"]已提交成功！", new Date(), userId);
		}
		return id;
	}

	@Override
	public void modify(Integer orderId, String title, String reason,
			Date startTime, Date endTime, Integer totalHours,
			Integer categoryId, Integer userId) {
		absenceOrderValue = this.queryByOrderId(orderId);
		absenceOrderValue.setTitle(title);
		absenceOrderValue.setReason(reason);
		absenceOrderValue.setStartTime(startTime);
		absenceOrderValue.setEndTime(endTime);
		absenceOrderValue.setTotalHours(totalHours);
		absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		absenceOrderDao.modify(absenceOrderValue);

	}
	
	@Override
	public Boolean nextCheck(Integer orderId) {
		absenceOrderValue = this.queryByOrderId(orderId);
		int userPostId = absenceOrderValue.getUserItem().getUserInfo().getPostInfo().getPostId();
		int currentPostId = absenceOrderValue.getStatus();
		String processOrder = prcoessService.queryProcesses(userPostId, IDEDStatus.ENABLE).get(0).getProcessOrder();
		String[] processItems = processOrder.split(",");
		if(currentPostId == IOrderStatus.PREPARE){
			absenceOrderValue.setStatus(Integer.parseInt(processItems[0]));		
			absenceOrderDao.modify(absenceOrderValue);
			return false;
		}
		for(int index = 0; index < processItems.length; index++){
			if(currentPostId == Integer.parseInt(processItems[index])){
				if(index == processItems.length-1){
					absenceOrderValue.setStatus(IOrderStatus.PASS);
					absenceOrderDao.modify(absenceOrderValue);
					messageInfoService.add("请假单审核通过", "["+absenceOrderValue.getTitle()+"]已审核通过！", new Date(), absenceOrderValue.getUserItem().getUserId());
					return true;
				}else{
					absenceOrderValue.setStatus(Integer.parseInt(processItems[index+1]));
					absenceOrderDao.modify(absenceOrderValue);
					return false;
				}
			}
		}
		return null;
	}
	
	@Override
	public void toFail(Integer orderId) {
		absenceOrderValue = this.queryByOrderId(orderId);
		absenceOrderValue.setStatus(IOrderStatus.FAILED);
		absenceOrderDao.modify(absenceOrderValue);
		messageInfoService.add("请假单审核未通过", "["+absenceOrderValue.getTitle()+"]已审核未通过！", new Date(), absenceOrderValue.getUserItem().getUserId());
	}

	@Override
	public String cancelAbsenceOrder(Integer orderId) {
		absenceOrderValue = this.queryByOrderId(orderId);
		absenceOrderValue.setStatus(IOrderStatus.CANCEL);
		absenceOrderDao.modify(absenceOrderValue);
		messageInfoService.add("请假单撤销成功", "["+absenceOrderValue.getTitle()+"]已撤销成功！", new Date(), absenceOrderValue.getUserItem().getUserId());
		return absenceOrderValue.getUserItem().getUserName()+","+absenceOrderValue.getTitle();
	}

	@Override
	public AbsenceOrderValue queryByOrderId(Integer orderId) {
		return absenceOrderDao.queryByOrderId(orderId);
	}

	@Override
	public List<AbsenceOrderValue> queryAbsenceOrder(Integer categoryId,
			Integer userId, Integer status, Integer rows, Integer pageNo) {
		if(categoryId != null)
			absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		if(userId != null)
			absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		if(status != null)
			absenceOrderValue.setStatus(status);
		
		return absenceOrderDao.queryAbsenceOrder(absenceOrderValue, rows, pageNo);
		
	}
	public Integer getTotalPageNumber(Integer categoryId, Integer userId, Integer status, Integer rows) {
		if(categoryId != null)
			absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		if(userId != null)
			absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		if(status != null)
			absenceOrderValue.setStatus(status);
		
		return absenceOrderDao.getTotalPageNumber(absenceOrderValue, rows);
				
	}
	
	@Override
	public Integer queryAbsenceOrderByDate(Integer categoryId, Integer userId,
			String startTime, String endTime) {
		if(categoryId != null)
			absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		if(userId != null)
			absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		int totalHours = 0;
		for(AbsenceOrderValue absenceOrder:absenceOrderDao.queryAbsenceOrderByDate(absenceOrderValue, startTime, endTime))
			totalHours += absenceOrder.getTotalHours();
		return totalHours;
	}

	@Override
	public Integer queryAbsenceOrderByMonth(Integer categoryId, Integer userId,
			String month) {
		if(categoryId != null)
			absenceOrderValue.setAbsenceCategory(absenceCategoryService.queryByCategoryId(categoryId));
		if(userId != null)
			absenceOrderValue.setUserItem(usersService.queryByUserId(userId));
		int totalHours = 0;
		for(AbsenceOrderValue absenceOrder:absenceOrderDao.queryAbsenceOrderByMonth(absenceOrderValue, month))
			totalHours += absenceOrder.getTotalHours();
		return totalHours;
	}
	
	//Getter and Setter

	public IAbsenceOrderDao getAbsenceOrderDao() {
		return absenceOrderDao;
	}

	public void setAbsenceOrderDao(IAbsenceOrderDao absenceOrderDao) {
		this.absenceOrderDao = absenceOrderDao;
	}

	public AbsenceOrderValue getAbsenceOrderValue() {
		return absenceOrderValue;
	}

	public void setAbsenceOrderValue(AbsenceOrderValue absenceOrderValue) {
		this.absenceOrderValue = absenceOrderValue;
	}

	public IAbsenceCategoryService getAbsenceCategoryService() {
		return absenceCategoryService;
	}

	public void setAbsenceCategoryService(
			IAbsenceCategoryService absenceCategoryService) {
		this.absenceCategoryService = absenceCategoryService;
	}

	public IUsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}

	public IProcessesService getPrcoessService() {
		return prcoessService;
	}

	public void setPrcoessService(IProcessesService prcoessService) {
		this.prcoessService = prcoessService;
	}



	public IMessageInfoService getMessageInfoService() {
		return messageInfoService;
	}

	public void setMessageInfoService(IMessageInfoService messageInfoService) {
		this.messageInfoService = messageInfoService;
	}

	public IReplyOrderService getReplyOrderService() {
		return replyOrderService;
	}

	public void setReplyOrderService(IReplyOrderService replyOrderService) {
		this.replyOrderService = replyOrderService;
	}


}
